package com.buss.admin.controller;

import com.buss.admin.entity.AdminEntity;
import com.buss.auth.transfer.vo.authen.AuthUserVO;
import com.buss.auth.transfer.vo.authen.AuthUserVOAdmin;
import com.buss.common.service.SysServiceI;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.jweb.core.util.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 管理员操作相关
 * 
 * @author 荆志宝
 *
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private SysServiceI sysService;

	@RequestMapping("/home")
	@RequiresAuthentication
	public String goHome() {
		return "/admin/home/home";

	}

	@RequestMapping("/changePwd")
	@ResponseBody
	public Object changePwd(String password) {
		Map result = new HashMap();
		try{
			Subject currentUser = SecurityUtils.getSubject();
			AuthUserVOAdmin user = (AuthUserVOAdmin) currentUser.getSession().getAttribute("userInfoAdmin");
			String userid = user.getId();
			AdminEntity adminEntity = this.sysService.get(AdminEntity.class, userid);
			adminEntity.setPassword(PasswordUtil.encrypt(password, PasswordUtil.str, PasswordUtil.getStaticSalt()));
			this.sysService.saveOrUpdate(adminEntity);
			result.put("data", 1);
			result.put("message", "修改密码成功");
		} catch (Exception e) {
			result.put("data", 500);
			result.put("message", "修改密码不成功");
		}
		return result;

	}

}
